package qq.activity;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.imu.appdevexperiment2022.R;
import cn.imu.appdevexperiment2022.start.BaseActivity;


public class RegisterTestActivity extends BaseActivity {
    private static final String TAG = "RegisterTestActivity";
    @BindView(R.id.til_username)
    TextInputLayout tilUsername;
    @BindView(R.id.til_nickname)
    TextInputLayout tilNickname;
    @BindView(R.id.til_password)
    TextInputLayout tilPassword;
    @BindView(R.id.til_repassword)
    TextInputLayout tilRepassword;
    @BindView(R.id.til_email)
    TextInputLayout tilEmail;
    @BindView(R.id.til_classid)
    TextInputLayout tilClassid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {

    }

    @OnClick(R.id.btn_register)
    public void register(){
        String username=tilUsername.getEditText().getText().toString();
        String nickname=tilNickname.getEditText().getText().toString();
        String password=tilPassword.getEditText().getText().toString();
        String repassword=tilRepassword.getEditText().getText().toString();
        String email=tilEmail.getEditText().getText().toString();
        String classid=tilClassid.getEditText().getText().toString();
        if (password.equals(repassword)){
            RegisterSendBean registerSendBean=new RegisterSendBean();
            registerSendBean.setUserName(username);
            registerSendBean.setPassword(password);
            registerSendBean.setNickName(nickname);
            registerSendBean.setClassesId(classid);
            registerSendBean.setEmail(email);
            Call<RegisterResultBean> call = ApiClient.getInstance(getApplicationContext())
                    .getReturnData(AuthUtil.GetAuth(),registerSendBean);
            call.enqueue(new Callback<RegisterResultBean>() {
                @Override
                public void onResponse(Call<RegisterResultBean> call, Response<RegisterResultBean> response) {
                    if (response.body()!=null){
                        if (response.body().getCode()==200){
                            Log.e(TAG,response.body().getMsg().toString());
                            ToastUtil.ShowToast(getApplicationContext(),"注册成功");
                            finish();
                        }else {
                            ToastUtil.ShowToast(getApplicationContext(),response.body().getMsg().toString());
                        }
                    }else{
                        ToastUtil.ShowToast(getApplicationContext(),"请求错误");
                    }
                }
                @Override
                public void onFailure(Call<RegisterResultBean> call, Throwable t) {
                    ToastUtil.ShowToast(getApplicationContext(), "网络请求错误");
                }
            });
        }else{
            ToastUtil.ShowToast(this,"两次输入的密码不一致");
        }
    }
    @OnClick(R.id.iv_back)
    public void reback(){
        finish();
    }
}
